App.controller('salaryPayRollController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
	
	 // 查询工资单的年份
	 Page.ajaxGet('salaryPayRoll/getSalaryPayRollYear.do', null, function(resp) {
	        $scope.salaryPayRollYearList = resp.data;
	    });
    $scope.grid = {
        add : function() {
            $scope.salaryPayRoll = {};
            $scope.modal.show();
        },
        colsePayRoll: function(row) {
           $scope.salaryPayRoll = row;
           $scope.salaryPayRoll.status = 2;
           Page.ajaxPost('salaryPayRoll/update.do', $scope.salaryPayRoll, function() {
               $scope.grid.query();
           });
        },
        save : function() {
            Page.ajaxPost($scope.salaryPayRoll.payrollId ? 'salaryPayRoll/update.do' : 'salaryPayRoll/add.do', $scope.salaryPayRoll, function() {
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        viewPayRollDetail : function(row) {
        	$scope.isDetail = true;
        	$scope.payRollYear = row.payrollYear;
        	$scope.payRollMonth = row.payrollMonth;
        },
        column_defs: [
		 /*{
			    target: 2,
			    template: function(row) {
			        return '<font color="red">xxxx</font>'
			    }
		 },*/
          {
            target : 3,
            buttons : [
                {
                    name: '工资明细',
                    class: 'btn-info',
                    icon: 'fa-reorder',
                    event: 'viewPayRollDetail'
                },
                {
                    name: '冻结',
                    class: 'btn-danger',
                    icon: 'fa-gavel',
                    event: 'colsePayRoll',
                    visiable: function(row) {
                        return row.status == '1';
                    }
                }
            ]
        }]
    };
    
    /*员工工资明细*/
    
    $scope.detailGrid = {
    		/* options : {
    	            bAutoWidth : false,
    	            scrollX : true,
    	            fixedColumns: {
    	                leftColumns : 3
    	            }
    	     },*/
            add : function() {
                $scope.salaryPayRollDetail = {};
                $scope.detailModal.show();
            },
            update: function(row) {
                $scope.salaryPayRollDetail = row;
                $scope.detailModal.show();
            },
            save : function() {
                Page.ajaxPost($scope.salaryPayRollDetail.paySlipId ? 'salaryPayRollDetail/update.do' : 'salaryPayRollDetail/add.do', $scope.salaryPayRollDetail, function() {
                    $scope.detailGrid.query();
                    $scope.detailModal.hide();
                });
            },
            reset : function(){
            	 $scope.empName="";
            	 $scope.detailGrid.query();
            },
            goBack : function(){
            	$scope.isDetail = false;
            	$scope.payRollYear = "";
            	$scope.payRollMonth = "";
            },
            showEmpQuotaDetail : function(row){
            	var param = {};
            	param.empId = row.empId;
            	param.payRollYear = $scope.payRollYear;
            	param.payRollMonth = $scope.payRollMonth;
            	 Page.ajaxGet('salaryPayRollDetail/getEmpQuotaDetail.do', param, function(resp) {
         	        $scope.empQuotaList = resp.data;
         	        $scope.empQuotaDetailModal.show();
         	    });
            },
            column_defs: [
    			{
    			    target: 0,
    			    template: function(row) {
    			    	var payrollYear = row.payrollYear;
    			    	var payrollMonth = row.payrollMonth;
    			    	if(payrollMonth<10){
    			    		payrollMonth = "0"+payrollMonth;
    			    	}
    			    	var salaryMonth = payrollYear+"年"+payrollMonth+"月";
    			        return '<font>'+salaryMonth+'</font>'
    			    }
    			},
              {
                target : 14,
                buttons : [
                    {
                        name: '编辑',
                        class: 'btn-warning btm-sm',
                        icon: 'fa-edit',
                        event: 'update',
                        visiable: function(row) {
                            return row.payRollStatus == '1';
                        }
                    }
                    ,
                    {
                        name: '定额明细',
                        class: 'btn-warning btm-sm',
                        icon: 'fa-edit',
                        event: 'showEmpQuotaDetail',
                        visiable: function(row) {
                            return row.payRollStatus == '1';
                        }
                    }
                    ,
                ]
            }],
            changeSalaryPayRollYear : function(){
        	 // 查询工资单年份对应的月份
        	 var param = {};
        	 param.salaryPayRollYear = $scope.payRollYear;
           	 Page.ajaxGet('salaryPayRoll/getSalaryPayRollMonthByYear.do', param, function(resp) {
           	        $scope.salaryPayRollMonthList = resp.data;
           	    });
            },
            changeValue : function(){// 修改考勤扣款、所得税
            	//实发工资=应发工资+提成-考勤扣款-所得税
            	var payableSalary = $scope.salaryPayRollDetail.payableSalary;// 应发工资
            	var deduction = parseFloat($scope.salaryPayRollDetail.deduction);// 提成
            	var attendanceDeductions = parseFloat($scope.salaryPayRollDetail.attendanceDeductions);// 考勤扣款
            	var tax = parseFloat($scope.salaryPayRollDetail.tax);// 所得税
            	
            	$scope.salaryPayRollDetail.paidSalary = payableSalary + deduction - attendanceDeductions - tax ;
            }
        };
}]);